<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="考勤状态" prop="type">
        <el-select v-model="queryParams.state" placeholder="请选择考勤状态" clearable>
          <el-option
              v-for="dict in dict.type.gigi_record_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['gigi.record:record:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-pie-chart"
            size="mini"
            @click="showPie"
        >统计图
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column label="学生姓名" align="center" prop="userName"/>
      <el-table-column label="学生学号" align="center" prop="userNo"/>
      <el-table-column label="学生班级" align="center" prop="userClass"/>
      <el-table-column label="考勤状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gigi_record_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="签到时间" align="center" prop="signInTime"/>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改考勤记录管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="签到id" prop="signInId">
          <el-input v-model="form.signInId" placeholder="请输入签到id"/>
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id"/>
        </el-form-item>
        <el-form-item label="考勤状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入考勤状态"/>
        </el-form-item>
        <el-form-item label="签到时间" prop="signInTime">
          <el-date-picker clearable
                          v-model="form.signInTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择签到时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="考勤照片" prop="pic">
          <el-input v-model="form.pic" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="isShow" width="500px">
      <div style="width: 100%;height: 500px;" ref="pie"></div>
    </el-dialog>

  </div>
</template>

<script>
import {listRecord, getRecord, delRecord, addRecord, updateRecord} from "@/api/gigi.record/record";
import * as echarts from "echarts";

export default {
  name: "Record",
  dicts: ['gigi_record_state'],
  data() {
    return {
      isShow: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤记录管理表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        signInId: null,
        userId: null,
        state: null,
        signInTime: null,
        pic: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        signInId: [
          {required: true, message: "签到id不能为空", trigger: "blur"}
        ],
        userId: [
          {required: true, message: "用户id不能为空", trigger: "blur"}
        ],
        state: [
          {required: true, message: "考勤状态不能为空", trigger: "blur"}
        ],
        pic: [
          {required: true, message: "考勤照片不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  watch:{
    '$route.query.signInId': 'getList'
  },
  methods: {
    showPie(){
      setTimeout(()=>{
        this.$refs.pie ? this.createPie() : '';
      },1000);
      this.isShow = true;
    },
    createPie() {
      let showCount = this.recordList.filter(e => e.state === 0).length;
      let unShowCount = this.recordList.filter(e => e.state === 1).length;
      let lateCount = this.recordList.filter(e => e.state === 2).length;
      let askCount = this.recordList.filter(e => e.state === 3).length;

      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(this.$refs.pie);

      // 指定图表的配置项和数据
      var option = {
        title: {
          text: '考勤统计图',
          x: 'center'//水平居中
        },
        tooltip: {//提示框组件
          trigger: 'item',
          formatter: "{a}<br/>{b}:{c} ({d}%)"
        },
        color: ['#FFC0CB','#87CEFA','#7B68EE', '#1E90FF'],
        series: [
          {
            name: '考勤状态',
            type: 'pie',
            radius: '55%',
            center: ['50%', '60%'],
            data: [
              {value: showCount, name: '正常'},
              {value: unShowCount, name: '缺勤'},
              {value: lateCount, name: '迟到'},
              {value: askCount, name: '请假'}
            ],
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0,0,0,0.5)'
              }
            }
          }
        ]
      };

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);

      console.log(this.$refs.pie)

      clearTimeout();
    },
    /** 查询考勤记录管理列表 */
    getList() {
      this.queryParams.signInId = this.$route.query.signInId;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        signInId: null,
        userId: null,
        state: null,
        signInTime: null,
        pic: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考勤记录管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考勤记录管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除考勤记录管理编号为"' + ids + '"的数据项？').then(function () {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gigi.record/record/export', {
        id: this.queryParams.signInId
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
